/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package no.ffi.tpm;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Federico Mancinie <fma at ffi.no>
 */
public class Config {
        
    static java.util.Properties props;
    static String fileName;
    public static final String SIGNATURE_ALGORITHM="sha1withrsa";
    public static final String ENCRYPTION_ALGORITHM="rsa";
    public static final String SYMMETRIC_CRYPTO_ALGORITHM="aes";

    private static void loadPropertiesFrom(String user) {
        if (props!=null) return; // Initialize only once
        props = new Properties();
        fileName=user+"_tpm.properties";
        if (user==null || user.equals(""))
            fileName = "tpm.properties";
        FileInputStream f = null;
        try {
            System.out.println(fileName);
            f = new FileInputStream(fileName);
            props.load(f);
            System.out.println("Properties loaded from " + user + ":");
            props.list(System.out);
        } catch (IOException ex) { 
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                f.close();
            } catch (IOException ex) {
                Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    }

    public static String getProperty(String name) {
        return getProperty(name,null);
    }
    
    public static String getProperty(String name, String def) {
        if (props==null) System.out.println("Props er null!");
        String s = props.getProperty(name, def);
        if (s == null) { 
            System.out.println("Null retur for " + name + " property");return null;
        }
        else return s.trim();
    }
    
    public static void setProperty(String name,String value){
        if (props==null) System.out.println("Props er null!");
        props.setProperty(name, value);
        System.out.println("The property "+name+" has been updated with value "+ value);
       
        try {
            FileOutputStream file = new FileOutputStream(fileName); 
            props.store(file,"Updated "+name);
            /*FileInputStream f = new FileInputStream(fileName);
            props.load(f);
            System.out.println("Properties loaded after update");
            props.list(System.out);
            f.close();*/
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } 
                
                
        
    }
    public static void  store(){
        try {
            FileOutputStream file = new FileOutputStream(fileName); 
            props.store(file,"Updated ");
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static Properties createDefault(String user){
        fileName=user+"_tpm.properties";
        FileOutputStream file = null;
        try {
            file =  new FileOutputStream(fileName);
            props=new Properties();
            props.setProperty("USER", user);
            //props.setProperty("TPM-PASSWORD", "TPMpassword");
            props.setProperty("AIK-UUID", "");
            props.setProperty("AIK-SNR", "");
            props.setProperty("AIK-PASS", "");
            props.setProperty("USER-KEYSTORE", "");
            props.setProperty("USER-KEYSTORE-PASS", "");
            props.setProperty("USER-CERT", "");
            props.setProperty("KEYSTORE_FILENAME","EdwardFongen.p12");
            props.setProperty("KEYSTORE_PASSWORD","anders");
            props.setProperty("TRUSTANCHOR_FILENAME","idp.ffi.no.pem");
            props.setProperty("IDP_URL","http://193.156.33.217:8084/IdmServlets/IdP");
            props.setProperty("MAP_SERVICE_URL","http://193.156.33.217:8084/IdmServlets/App");
            props.setProperty("MAP_SERVICE_ID","mapserver.ffi.no");
            props.setProperty("MAP_PROTOCOL_CLASS"," stateless");
            //props.setProperty("LEGACY-KEY-UUID", "");
            //props.setProperty("LEGACY-KEY-PASS", "");
            props.setProperty("CA-CERT", "cacert.pem");
            props.store(file, "Properties file for user "+user);
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        
        }
        return props;
    }
}
